import { xhr_get, xhr_post_json } from './request';
import { getUrl } from '@ey-utils/utils/services/index';

export let allData: any = {}; //临时数据存储

//获取全部数据（不缓存）
export const getTempData = (api?: any) => {
  return new Promise((resolve: any, reject: any) => {
    if (api?.type?.toLowerCase() == 'post') {
      xhr_post_json(getUrl(api.url), api.params, undefined, api.cache)
        .then((res: any) => {
          if (res.result) {
            resolve(res.result);
            return;
          }
          reject(res);
        })
        .catch((err) => reject(err));
    } else if (api?.type?.toLowerCase() == 'get') {
      xhr_get(getUrl(api.url), api.params, undefined, api.cache)
        .then((res: any) => {
          if (res.result) {
            resolve(res.result);
            return;
          }
          reject(res);
        })
        .catch((err) => reject(err));
    } else {
      xhr_get(getUrl(api.url), api.params,  undefined, api.cache)
        .then((res: any) => {
          if (res.result) {
            resolve(res.result);
            return;
          }
          reject(res);
        })
        .catch((err) => reject(err));
    }
  });
};
